package com.zkingsoft.actions.admin;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.httpobjects.Request;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zkingsoft.model.Statement;
import com.zkingsoft.model.TabClass;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.plugin.util.ExcelUtil;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.plugin.util.ExcelUtil.ViewExcel;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.StatementService;

@Controller
@RequestMapping(value = "admin/statement")
public class StatementController {
	@Resource
	private StatementService statementService;
	long deliveries;

	// static long tuition;
	@RequestMapping(value = "/inPage")
	public @ResponseBody AjaxResult inPage(Statement statement, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		List<Statement> list = statementService.findInPage(statement, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, list,
				statementService.findTotal(statement));
		return result;
	}

	@RequestMapping(value = "/findAll")
	public @ResponseBody AjaxResult findAll(Statement statement) {
		List<Statement> list = statementService.findByModel(statement);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, list,
				statementService.findTotal(statement));
		return result;
	}

	/**
	 * 
	 * @Description:导出excel
	 * @author:laotie
	 * @return 返回类型 ModelAndView
	 * @date 2017年5月11日
	 */
	@RequestMapping(value = "/exportExcel")
	public @ResponseBody ModelAndView report(ModelMap model, HttpServletRequest request, HttpServletResponse response,
			Statement statement) {
		try {
			if (statement.getClaName() != null && !(statement.getClaName().equals(""))) {
				statement.setClaName(java.net.URLDecoder.decode(statement.getClaName(), "UTF-8"));
			}
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		// 查询符合条件的数据
		List<Statement> dataList = statementService.findByModel(statement);
		// 把数据组装成我们要的数据，外面的list表示一行数据，也就是一个对象的信息，对应到excel的一行数据。里面的list表示对象的一个属性，对应excel的单元格
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FORMAT_DD);
		List<List<Object>> list = new ArrayList<>();
		for (Statement obj : dataList) {
				// 判断是否已交清
				if (obj.gettUnTuition() <= 0 && obj.gettInTuition() != 0) {
					obj.setIsNot("是");
				} else {
					obj.setIsNot("否");
				}
			List<Object> temp = new ArrayList<>();
			temp.add(obj.getStuNo());
			temp.add(obj.getStuName());
			temp.add(obj.getStuSchool());
			temp.add(obj.getStuIfschool());
			temp.add(obj.getClaName());
			temp.add(obj.getCouName());
			temp.add(obj.getPayable());
			temp.add(obj.gettInTuition());
			temp.add(obj.gettUnTuition());
			temp.add(obj.getIsNot());
			String stDate = sdf.format(obj.getStuDate());
			temp.add(stDate);
			temp.add(obj.getStuStatus());
			temp.add(obj.getTcName());
			temp.add(obj.getStuRemark());
			list.add(temp);
		}

		// 调用我们的excel工具类，导出数据
		HSSFWorkbook workbook = ExcelUtil.generateExcel("学费统计表", new String[] { "学号", "学生姓名", "来自院校", "是否院校毕业", "班级", "课程",
				"应交学费", "已交学费", "未交学费", "是否已交清", "入学时间", "状态", "班主任", "备注", }, list);
		try {
			ViewExcel.buildExcel(null, workbook, request, response, "统计学费列表.xls");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView(new ViewExcel(), model);
	}
}
